1 using UnityEngine;
2 using
System.Collections.Generic;
3 using
System.IO;
4 using
System.Xml;
5
6 public
class BitmapFont{
7     
private Dictionary<string, Sprite> sprites;
8     
private Dictionary<string, Vector2> yoffsets_xadvances;
9     
private Dictionary<string, Rect> rects;
10
11     
private GameObject gameObject;
12     
private GameObject fontObject;
13
14     
private Color color = new Color(1, 1, 1, 1);
15
16     
public float width;
17
18     
public BitmapFont(BitmapFont bitmapFont, GameObject gameObject)
19     {
20         
this.gameObject = gameObject;
21         
this.sprites = bitmapFont.getSprites();
22         
this.yoffsets_xadvances = bitmapFont.getOAs();
23         
this.rects = bitmapFont.getRects();
24     }
25
26     
public BitmapFont(string pathPNG, string pathXML, GameObject gameObject)
27     {
28         
this.gameObject = gameObject;
29         sprites =
new Dictionary<string, Sprite>();
30         yoffsets_xadvances =
new Dictionary<string, Vector2>();
31         rects =
new Dictionary<string, Rect>();
32
33         Texture2D texture = Resources.Load<Texture2D>(pathPNG);
34         
float height = texture.height;
35         TextAsset xml = Resources.Load<TextAsset>(pathXML);
36         
37         XmlDocument test =
new XmlDocument();
38         test.Load(
new StringReader(xml.text));
39         
40         
//test.LoadXml(new StringReader(xml.text).ReadToEnd());
41         
//string[] keys = new string[] {"x","y","width","height","yoffset","xadvance","letter"};
42
43         
foreach (XmlNode node in test.DocumentElement.ChildNodes)
44         {
45             XmlAttributeCollection collection = node.Attributes;
46             Rect rect =
new Rect(float.Parse(collection.Item(0).Value), height - float.Parse(collection.Item(1).Value) - float.Parse(collection.Item(3).Value), float.Parse(collection.Item(2).Value), float.Parse(collection.Item(3).Value));
47             rects.Add(collection.Item(
6).Value, rect);
48             sprites.Add(collection.Item(
6).Value, Sprite.Create(texture, rect, Vector2.zero));
49             yoffsets_xadvances.Add(collection.Item(
6).Value, new Vector2(float.Parse(collection.Item(4).Value), float.Parse(collection.Item(5).Value)));
50         }
51     }
52
53     
public void setColor(Color color)
54     {
55         
this.color = color;
56     }
57
58     
public void setText(string text, float kerning, float space)
59     {
60         
float k = kerning / 100;
61         
float sp = space / 100;
62         
if (fontObject != null)
63             Object.Destroy(fontObject);
64
65         
this.fontObject = new GameObject("BitmapFont");
66         
this.fontObject.transform.parent = this.gameObject.transform;
67         
//this.fontObject.transform.localPosition = new Vector3(0, 0, fontObject.transform.localPosition.z);
68         
this.fontObject.transform.localPosition = new Vector3(0, 0, 0);
69
70         
if (text.Trim().Equals(""))
71         {
72             removeChildren();
73             
return;
74         }
75
76         
float width = 0;
77         {
78             
string c = text[0].ToString();
79             GameObject go =
new GameObject(c);
80             go.AddComponent<SpriteRenderer>().sprite = sprites[c];
81             go.GetComponent<SpriteRenderer>().color = color;
82             go.transform.parent = fontObject.transform;
83             Vector2 p = yoffsets_xadvances[c];
84             
//go.transform.localPosition = new Vector3(width, p.x/100, go.transform.localPosition.z);
85             
//go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);
86             
//go.transform.localPosition = new Vector3(width, p.x/100 -rects[c].height/200, 0);
87             go.transform.localPosition =
new Vector3(width, -rects[c].height/200, 0);
88             
//go.transform.localPosition = new Vector3(width, -p.x / 100 + rects[c].height/200, 0);
89             width += p.y /
100 + k;
90         }
91
92         
for (int i = 1; i < text.Length; i++)
93         {
94             
string c = text[i].ToString();
95             
if (c == " ")
96             {
97                 width += sp;
98             }
99             
else
100             {
101                 GameObject go =
new GameObject(c);
102                 go.AddComponent<SpriteRenderer>().sprite = sprites[c];
103                 go.GetComponent<SpriteRenderer>().color = color;
104                 go.transform.parent = fontObject.transform;
105
106                 Rect rect = rects[c];
107                 Vector2 p = yoffsets_xadvances[c];
108                 
//go.transform.localPosition = new Vector3(width, p.x/100, go.transform.localPosition.z);
109                 
//go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);
110                 
//go.transform.localPosition = new Vector3(width, p.x/100 - rect.height/200, 0);
111                 go.transform.localPosition =
new Vector3(width, -rect.height/200, 0);
112                 
//go.transform.localPosition = new Vector3(width, -p.x/100 + rects[c].height/200, 0);
113                 width += p.y /
100 + k;
114             }
115         }
116         
this.width = width;
117         fontObject.transform.localPosition -=
new Vector3(width/2, 0, 0);
118     }
119
120     
public void setText(string text, float kerning, float space, string layerName, string sortingLayerName)
121     {
122         
float k = kerning / 100;
123         
float sp = space / 100;
124         
if (fontObject != null)
125             Object.Destroy(fontObject);
126
127         
this.fontObject = new GameObject("BitmapFont");
128         
this.fontObject.transform.parent = this.gameObject.transform;
129         
this.fontObject.layer = LayerMask.NameToLayer(layerName);
130         
//this.fontObject.transform.localPosition = new Vector3(0, 0, fontObject.transform.localPosition.z);
131         
this.fontObject.transform.localPosition = new Vector3(0, 0, 0);
132
133         
if (text.Trim().Equals(""))
134         {
135             removeChildren();
136             
return;
137         }
138
139         
float width = 0;
140         {
141             
string c = text[0].ToString();
142             GameObject go =
new GameObject(c);
143             go.layer = LayerMask.NameToLayer(layerName);
144             go.AddComponent<SpriteRenderer>().sprite = sprites[c];
145             go.GetComponent<SpriteRenderer>().color = color;
146             go.GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
147             go.transform.parent = fontObject.transform;
148             Vector2 p = yoffsets_xadvances[c];
149             
//go.transform.localPosition = new Vector3(width, p.x/100, go.transform.localPosition.z);
150             
//go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);
151             
//go.transform.localPosition = new Vector3(width, p.x/100 -rects[c].height/200, 0);
152             go.transform.localPosition =
new Vector3(width, 0, 0);
153             
//go.transform.localPosition = new Vector3(width, -p.x / 100 + rects[c].height/200, 0);
154             width += p.y /
100 + k;
155         }
156
157         
for (int i = 1; i < text.Length; i++)
158         {
159             
string c = text[i].ToString();
160             
if (c == " ")
161             {
162                 width += sp;
163             }
164             
else
165             {
166                 GameObject go =
new GameObject(c);
167                 go.layer = LayerMask.NameToLayer(layerName);
168                 go.AddComponent<SpriteRenderer>().sprite = sprites[c];
169                 go.GetComponent<SpriteRenderer>().color = color;
170                 go.GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
171                 go.transform.parent = fontObject.transform;
172
173                 Rect rect = rects[c];
174                 Vector2 p = yoffsets_xadvances[c];
175                 
//go.transform.localPosition = new Vector3(width, p.x/100, go.transform.localPosition.z);
176                 
//go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);
177                 
//go.transform.localPosition = new Vector3(width, p.x/100 - rect.height/200, 0);
178                 go.transform.localPosition =
new Vector3(width, 0, 0);
179                 
//go.transform.localPosition = new Vector3(width, -p.x/100 + rects[c].height/200, 0);
180                 width += p.y /
100 + k;
181             }
182         }
183         
this.width = width;
184         
//fontObject.transform.localPosition -= new Vector3(width / 2, 0, 0);
185     }
186
187     
private void removeChildren()
188     {
189         
if (fontObject.transform.childCount > 0)
190         {
191             Transform[] children = fontObject.GetComponentsInChildren<Transform>();
192             
for (int i = 0; i < children.Length; i++)
193                 Object.Destroy(children[i].gameObject);
194         }
195     }
196
197     
public Dictionary<string, Sprite> getSprites()
198     {
199         
return this.sprites;
200     }
201
202     
public Dictionary<string, Vector2> getOAs()
203     {
204         
return this.yoffsets_xadvances;
205     }
206
207     
public Dictionary<string, Rect> getRects()
208     {
209         
return rects;
210     }
211 }


test.LoadXml(new StringReader(xml.text).ReadToEnd());

string[] keys = new string[] {"x","y","width","height","yoffset","xadvance","letter"};

this.fontObject.transform.localPosition = new Vector3(0, 0, fontObject.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100 -rects[c].height200, 0);

go.transform.localPosition = new Vector3(width, -p.x 100 + rects[c].height200, 0);

go.transform.localPosition = new Vector3(width, p.x100, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100 - rect.height200, 0);

go.transform.localPosition = new Vector3(width, -p.x100 + rects[c].height200, 0);

this.fontObject.transform.localPosition = new Vector3(0, 0, fontObject.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100 -rects[c].height200, 0);

go.transform.localPosition = new Vector3(width, -p.x 100 + rects[c].height200, 0);

go.transform.localPosition = new Vector3(width, p.x100, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, 0, go.transform.localPosition.z);

go.transform.localPosition = new Vector3(width, p.x100 - rect.height200, 0);

go.transform.localPosition = new Vector3(width, -p.x100 + rects[c].height200, 0);

fontObject.transform.localPosition -= new Vector3(width 2, 0, 0);




Trò chơi đua xe động vật trong UNITY Engine 114.741 lượt xem

Gõ tìm kiếm nhanh...